﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HandBook.Data;
using HandBook.Common;
using Telerik.Web.UI;

public partial class Admin_NewsManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void uxNews_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        fillAllData();
    }

    private void fillAllData()
    {
        var newsTemp = from newid in HandBookData.EatDrinkDB.News
                       select newid;
        uxNews.DataSource = newsTemp;
    }
    protected void uxNews_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        New id = new New();
        int styleID = Convert.ToInt32((e.Item.FindControl("dllNewsStyleName") as DropDownList).SelectedValue);
        if (!styleID.Equals("select"))
        {
            id.NewsStyleID = styleID;
        }
        id.NewsDate = Convert.ToDateTime(DateTime.Now.ToString());
        id.NewsTitle = (e.Item.FindControl("txtTitle") as TextBox).Text.Trim();
        id.NewsShort = (e.Item.FindControl("radEditorShort") as RadEditor).Content;
        id.NewsLong = (e.Item.FindControl("radEditorLong") as RadEditor).Content;
        id.NewsPath = (e.Item.FindControl("txtPath") as TextBox).Text.Trim();
        Boolean show = Convert.ToBoolean((e.Item.FindControl("ddlIShowHomePage") as DropDownList).SelectedValue);
        if (!show.Equals("select"))
        {
            id.IsShowHomePage = show;
        }
        HandBookData.EatDrinkDB.News.InsertOnSubmit(id);
        HandBookData.EatDrinkDB.SubmitChanges();

    }
    protected void uxNews_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int newsid = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["NewsID"]);
        var id = HandBookData.EatDrinkDB.News.Single(f => f.NewsID == newsid);
        HandBookData.EatDrinkDB.News.DeleteOnSubmit(id);
        HandBookData.EatDrinkDB.SubmitChanges();

    }
    protected void uxNews_CancelCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        fillAllData();
    }
    protected void uxNews_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int newsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["NewsID"]);

        var id = HandBookData.EatDrinkDB.News.Single(f => f.NewsID == newsID);
        int styleID = Convert.ToInt32((e.Item.FindControl("dllNewsStyleName") as DropDownList).SelectedValue);
        if (!styleID.Equals("select"))
        {
            id.NewsStyleID = styleID;
        }
        id.NewsTitle = (e.Item.FindControl("txtTitle") as TextBox).Text.Trim();
        id.NewsShort = (e.Item.FindControl("radEditorShort") as RadEditor).Content;
        id.NewsLong = (e.Item.FindControl("radEditorLong") as RadEditor).Content;
        id.NewsPath = (e.Item.FindControl("txtPath") as TextBox).Text.Trim();
        Boolean show = Convert.ToBoolean((e.Item.FindControl("ddlIShowHomePage") as DropDownList).SelectedValue);
        if (!show.Equals("select")) 
        {
            id.IsShowHomePage = show;
        }         
        HandBookData.EatDrinkDB.SubmitChanges();
    }
    protected void uxNews_NeedDataSource1(object source, GridNeedDataSourceEventArgs e)
    {
        var NewList = from Newtem in HandBookData.EatDrinkDB.News
                      select Newtem;
        uxNews.DataSource = NewList;
    }
    protected void uxNews_DeleteCommand1(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int newsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["NewsID"]);
        var newsList = HandBookData.EatDrinkDB.News.Single(n => n.NewsID == newsID);
        HandBookData.EatDrinkDB.News.DeleteOnSubmit(newsList);
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}
